package com.spooky.sync.note.dialog;

import java.util.LinkedList;
import java.util.List;
import java.util.regex.Pattern;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import com.spooky.sync.R;
import com.spooky.sync.persistence.entity.Tag;

public class TagSelectDialog extends Dialog {
	private static final String TAG = "TagSelectDialog";
	// GUI
	private EditText mEditText;
	private final Context mContext;
	// BUNDLE KEY
	private static final String STORED_TAGS = "mTags";
	private static final String REGEX_PART = ",";
	// TMP STORAGE
	private String mTagStorage = null;
	// Tag
	private List<Tag> mTags = null;

	public TagSelectDialog(Context context, List<Tag> tags) {
		super(context);
		mContext = context;
		mTags = tags;
	}

	public TagSelectDialog(Context context) {
		super(context);
		mContext = context;
		mTags = new LinkedList<Tag>();
	}

	public TagSelectDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
		super(context, cancelable, cancelListener);
		mContext = context;
		mTags = new LinkedList<Tag>();
	}

	public TagSelectDialog(Context context, int theme) {
		super(context, theme);
		mContext = context;
		mTags = new LinkedList<Tag>();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tag_select_dialog);
		LayoutParams params = getWindow().getAttributes();
		params.height = LayoutParams.MATCH_PARENT;
		params.width = LayoutParams.MATCH_PARENT;
		getWindow().setAttributes(params);
		if (savedInstanceState != null) {
			mTagStorage = savedInstanceState.getString(STORED_TAGS);
		}
		mTagStorage = mTagStorage == null ? "" : mTagStorage;
		mEditText = (EditText) findViewById(R.id.tagSelector);
		mEditText.setSingleLine(false);
		setTitle("Tag Select");
		final Button back = (Button) findViewById(R.id.tagBackSelector);
		final Button confirm = (Button) findViewById(R.id.tagConfirmSelector);
	}

	@Override
	public Bundle onSaveInstanceState() {
		Bundle saveInstanceState = super.onSaveInstanceState();
		mTagStorage = mEditText.getText().toString();
		saveInstanceState.putString(STORED_TAGS, mTagStorage);
		return saveInstanceState;
	}

	public List<Tag> getTags() {
		mTags = assemble();
		return mTags;
	}

	public void setTags(List<Tag> tags) {
		mTags = tags;
	}

	private List<Tag> assemble() {
		mTagStorage = mEditText.getText().toString();
		List<Tag> tagList = new LinkedList<Tag>();
		try {
			String tags = mTagStorage.replaceAll(",{2,}", ",");
			tags = tags.trim();
			String[] split = Pattern.compile(REGEX_PART).split(tags);
			Tag tag = null;
			for (String stringTag : split) {
				tag = new Tag();
				tag.setName(stringTag);
				tagList.add(tag);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return tagList;
	}
}
